import os
import re
import json
import numpy as np
import requests


def traverse_dir(path, tails, file_info_ls=None):
    """

    :param path:
    :param tails:
    :param file_info_ls:
    :return: [(文件路径1，文件名1),(文件路径2，文件名2),(文件路径3，文件名3),...,(文件路径n，文件名n)]
    注意：如路经太深python就无法检测该文件了，遇到这种情况请开启 clean_path功能；
    参考： https://blog.csdn.net/baidu_34928905/article/details/131935428
    """
    path = path
    if file_info_ls is None:
        file_info_ls = set()
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            traverse_dir(file_path, tails, file_info_ls)
        else:
            file_e_ls = os.path.basename(file_path).split('.')
            file_tail = file_e_ls[-1]
            if (tails is None or file_tail in tails) and not os.path.basename(file_path).startswith('~$'):
                item = (file_path, '.'.join(file_e_ls[0:-1]))
                file_info_ls.add(item)
    return file_info_ls
